import java.util.ArrayList;
import java.util.Collections;
class Node {
    public long value;
    public Node next;
    public Node(long value) {
        this.value = value;
        this.next = null;
    }
}
public class sortedLinkedList {
    private Node head; // 链表头节点
    // 创建固定数据的链表
    public void createLinkedList() {
        Node node = new Node(111);
        Node next1 = new Node(333);
        Node next2 = new Node(9999);
        Node next3 = new Node(66);
        Node next4 = new Node(112);
        Node next5 = new Node(213);
        Node next6 = new Node(447);
        Node next7 = new Node(168);
        Node next8 = new Node(1);
        Node next9 = new Node(4321);
        Node next10 = new Node(8787);
        Node next11 = new Node(20240311044L);

        head = node;
        node.next = next1;
        next1.next = next2;
        next2.next = next3;
        next3.next = next4;
        next4.next = next5;
        next5.next = next6;
        next6.next = next7;
        next7.next = next8;
        next8.next = next9;
        next9.next = next10;
        next10.next = next11;
    }
    // 链表排序方法
    public void sort() {
        // 边界情况：空链表或单节点链表
        if (head == null || head.next == null) return;
        // 步骤1：收集所有节点值
        ArrayList<Long> values = new ArrayList<>();
        Node current = head;
        while (current != null) {
            values.add(current.value);
            current = current.next;
        }
        // 步骤2：使用内置排序算法
        Collections.sort(values);
        // 步骤3：重建有序链表
        head = new Node(values.get(0)); // 新头节点
        Node tmp = head;
        for (int i = 1; i < values.size(); i++) {
            tmp.next = new Node(values.get(i)); // 创建新节点
            tmp = tmp.next; // 移动指针
        }
    }
    // 显示链表内容（改进版）
    public void display() {
        Node tmp = head;
        System.out.print("[");  // 起始符号
        while (tmp != null) {
            System.out.print(tmp.value);
            // 动态判断是否需要添加箭头
            if (tmp.next != null) {
                System.out.print(" -> ");
            }
            tmp = tmp.next; // 移动到下一个节点
        }
        System.out.println("]"); // 结束符号
    }
    // 测试主方法
    public static void main(String[] args) {
        sortedLinkedList list = new sortedLinkedList();
        // 创建并显示原始链表
        list.createLinkedList();
        System.out.println("原始链表：");
        list.display();
        // 排序并显示结果
        list.sort();
        System.out.println("升序排序后：");
        list.display();
    }
}